/**
 * 2017年12月22日
 */
package exp.util;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
/**
 * @author Alex
 *
 */
public class ArffUtils {
	public static String sep = "_";
	public static void main(String[] args) throws Exception {
		for(int i = 9;i<10 ; i++){
			findArffSizeLessThan(i);
		}
	}
	
	public static void findArffSizeLessThan(int k) throws Exception {
		String path = "D:\\wekadataset\\timeseries\\datasets";
		int M = k;
		List<File> files = searchRootDir(path,M);
		String target = "D:\\wekadataset\\timeseries\\EXP_DATASET\\Less"+M+"M";
		File targetDir = new File(target);
		if(!targetDir.exists()){
			targetDir.mkdirs();
		}
		for(File f:files){
			FileUtils.copyFile(f, new File(targetDir,f.getName()));
		}
		
	}
	public static List<File> searchRootDir(String root,int sizeThresHoldInM)throws Exception{
		HashSet<String> names = new HashSet<String>();
		File rootDir = new File(root);
		if(rootDir.exists()){
			if(!rootDir.isDirectory()){
				throw new RuntimeException(root +" should a directory");
			}
			List<File> result = new ArrayList<>();
			search(rootDir,result,names,sizeThresHoldInM);
			
			String namesArr [] = names.toArray(new String[names.size()]);
			Arrays.sort(namesArr, 0, namesArr.length, (x,y)->x.compareTo(y));
			System.out.println(Arrays.toString(namesArr));
			return result;
		}else{
			throw new RuntimeException(root +" not exist");
		}
	}
	
	private static void search(File dir,List<File> collector,Set<String>names,int sizeThresHoldInM){
		if(dir.isFile()){
			String filenameWithTrainorTest = dir.getName();
			int pos = filenameWithTrainorTest.indexOf(sep);
			if(pos<0) return;
			String name = filenameWithTrainorTest.substring(0,pos);
			
			if(!names.contains(name)){
				
				String another = name + (
						((filenameWithTrainorTest.endsWith("arff")||(filenameWithTrainorTest.endsWith("ARFF"))?
							((filenameWithTrainorTest.endsWith("TRAIN.arff")||(filenameWithTrainorTest.endsWith("TRAIN.ARFF"))?"_TEST.arff":"_TRAIN.arff"))
							:
							((filenameWithTrainorTest.endsWith("TRAIN")||(filenameWithTrainorTest.endsWith("train"))?"_TEST":"_TRAIN"))
						))
				);
				File anotherFile = new File(dir.getParentFile(),another);
				if(dir.exists() && anotherFile.exists()){
					//MB
					long size = dir.length() /(1024*1024);
					long anotherSize = anotherFile.length()/(1024*1024);
					if(size<sizeThresHoldInM && anotherSize<sizeThresHoldInM){
						names.add(name);
						collector.add(dir);
						collector.add(anotherFile);
					}
				}
				
			}
		}else{
			File dirs[] = dir.listFiles();
			for(File f:dirs){
				search(f, collector, names,sizeThresHoldInM);
			}
		}
	}
}
